/** 
 * @Description: 三角锥光束效果
 * @Author: 程前
 * @Date: 2025-02-13 15:27:13
 * @LastEditors: 程前
 * @LastEditTime: 2025-03-11 09:50:23
 */

import { Viewer, Entity, Color, ScreenSpaceEventType, ScreenSpaceEventHandler } from "cesium"
import { setEventByHandler, removeEventByHandler, getAssetsFile } from "@/utils"
import { initCylinderBeamMaterial, CylinderBeamMaterialProperty } from "../../material/CylinderBeamMaterialProperty";
import { CallbackPositionProperty } from "cesium";
import { CallbackProperty } from "cesium";
import { Cartesian3 } from "cesium";

export default class CylinderBeam {
    private viewer: Viewer;
    private entity: Entity;
    private handler: ScreenSpaceEventHandler;
    private config: any;
    private materialProperty: CylinderBeamMaterialProperty;

    constructor(viewer: Viewer, handler: ScreenSpaceEventHandler) {
        this.viewer = viewer;
        this.handler = handler;
        this.config = {
            height: 1000,          // 高度
            bottomRadius: 400,     // 底部半径
            topRadius: 0,          // 顶部半径为0形成锥形
            speed: 0.6,             // 扩散速度
            bandWidth: 0.2,       // 光带宽度
            gradient: 0.01,        // 边缘渐变
            rotation: Math.PI / 4, // 旋转角度（弧度制）
            bands: 6,             // 光带数量
            baseColor: Color.WHITE.withAlpha(0.3),   // 锥体颜色
            lightColor: Color.fromCssColorString('#ff8c00'),  // 光带颜色
            outline: true,
            outlineColor: Color.WHITE
        };

        // 初始化材质
        initCylinderBeamMaterial();
        this.materialProperty = new CylinderBeamMaterialProperty({
            baseColor: this.config.baseColor,
            lightColor: this.config.lightColor,
            speed: this.config.speed,
            bandWidth: this.config.bandWidth,
            gradient: this.config.gradient,
            rotation: this.config.rotation,
            bands: this.config.bands
        });
    }

    /**
     * 初始化事件
     */
    initEvents() {
        const leftClick = (event: any) => {
            const ray = this.viewer.camera.getPickRay(event.position);
            const position = this.viewer.scene.globe.pick(ray, this.viewer.scene);
            this.createCylinderEntity(position);
        }
        setEventByHandler(this.handler, leftClick, ScreenSpaceEventType.LEFT_CLICK);
    }

    /**
     * 创建圆锥体实体
     * @param position 位置
     */
    public createCylinderEntity(position: any, orientation: any = undefined) {
        // 如果已存在实体，先移除
        if (this.entity) {
            this.viewer.entities.remove(this.entity);
        }

        this.entity = this.viewer.entities.add({
            position,
            orientation,
            cylinder: {
                length: this.config.height,
                topRadius: this.config.topRadius,
                bottomRadius: this.config.bottomRadius,
                material: this.materialProperty,
                outline: this.config.outline,
                outlineColor: this.config.outlineColor,
                numberOfVerticalLines: 4,
                slices: 4
            }
        });
        return this.entity
    }

    /**
     * 更新材质参数
     */
    updateMaterialConfig(options: {
        speed?: number;
        bandWidth?: number;
        gradient?: number;
        baseColor?: Color;
        lightColor?: Color;
        rotation?: number;
        bands?: number;
    }) {
        if (options.speed !== undefined) {
            this.materialProperty.speed = options.speed;
        }
        if (options.bandWidth !== undefined) {
            this.materialProperty.bandWidth = options.bandWidth;
        }
        if (options.gradient !== undefined) {
            this.materialProperty.gradient = options.gradient;
        }
        if (options.baseColor !== undefined) {
            this.materialProperty.baseColor = options.baseColor;
        }
        if (options.lightColor !== undefined) {
            this.materialProperty.lightColor = options.lightColor;
        }
        if (options.rotation !== undefined) {
            this.materialProperty.rotation = options.rotation;
        }
        if (options.bands !== undefined) {
            this.materialProperty.bands = options.bands;
        }
    }

    /**
     * 销毁
     */
    destroy() {
        if (this.handler) {
            this.handler.destroy();
        }
        if (this.entity) {
            this.viewer.entities.remove(this.entity);
        }
    }
}
